package com.gxa.business.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gxa.business.entity.Industry;
import com.gxa.business.mapper.IndustryMapper;
import com.gxa.business.service.IndustryService;
import com.gxa.common.base.RespResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.sql.Timestamp;
import java.util.Date;

@Service
@Slf4j
public class IndustryServiceImpl implements IndustryService {

    @Resource
    private IndustryMapper industryMapper;

    @Override
    public RespResult createIndustryInfo(String name, Byte status) {
        Timestamp timestamp = new Timestamp(new Date().getTime());
        Industry industry = new Industry(name,timestamp,status);
        industryMapper.insert(industry);
        return RespResult.success("新建行业类型成功！");
    }

    @Override
    public RespResult editIndustryInfo(String id,String name, Byte status) {
        Timestamp timestamp = new Timestamp(new Date().getTime());
        UpdateWrapper<Industry> industryUpdateWrapper = new UpdateWrapper<>();
        industryUpdateWrapper.eq("id",id).set("name",name).set("create_time",timestamp).set("status",status);
        industryMapper.update(null,industryUpdateWrapper);
        return RespResult.success("编辑行业类型成功！");
    }

    @Override
    public RespResult updateStatus(String id) {
        industryMapper.deleteById(id);
        return RespResult.success("下架成功！");
    }

    @Override
    public RespResult queryAllIndustryInfo(int currentPage, int pageSize) {
        Page<Industry> industryPage = new Page<>(currentPage,pageSize);
        IPage<Industry> industryIPage = industryMapper.selectPage(industryPage,null);
        return RespResult.success(industryIPage.getRecords());
    }
}
